﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UMO.UmoControls;
namespace UMO.UmoControls
{
    /// <summary>
    /// Interaction logic for UmoNodesTreeView.xaml
    /// </summary>
    public partial class UmoNodesTreeView : UserControl
    {
        public UmoNodesTreeView()
        {
            InitializeComponent();
        }
        private MainWindow ParentWindow;
        public UmoNodesTreeView(MainWindow parentWindow)
        {
            InitializeComponent();
            this.ParentWindow = parentWindow;
            this.DisableAddingFolders();
            this.DisableAddingItems();
            this.DisableItemsDeletion();
            //SetZeroLengthRestrictions();
            UpdateTreeView();            

        }
        private void DisableAddingItems()
        {
            this.AddFileButton.IsEnabled = false;
            this.AddUrlButton.IsEnabled = false;
        }
        private void DisableAddingFolders()
        {
            this.AddFolderButton.IsEnabled = false;
        }

        public void UpdateTreeView()
        {
            Dictionary<int, TreeViewItem> treeNodes = new Dictionary<int, TreeViewItem>();
            this.nodesView.Items.Clear();
            foreach (UmoNode item in this.ParentWindow.GraphNodes.Values)
            {
                TreeViewItem treeNode = new TreeViewItem();
                treeNode.Header = item.Value;
                treeNode.Tag = item;
                treeNode.IsExpanded = true;
                treeNodes.Add(item.Index, treeNode);
                if (treeNodes.ContainsKey(item.ParentIndex))
                {
                    treeNodes[item.ParentIndex].Items.Add(treeNode);
                }
                else
                {
                    this.nodesView.Items.Add(treeNode);
                }
            }
        }
        private static int GetAdditionID(int[] keyCollection)
        {            
            int valueToRet = 0;
            if (keyCollection.Length > 0)
            {
                valueToRet = keyCollection[0];
                foreach (int key in keyCollection)
                {
                    if (valueToRet < key)
                    {
                        valueToRet = key;
                    }
                }
            }
            return valueToRet+1;
        }
        public void AddFile()
        {
            int additionKey = UmoNodesTreeView.GetAdditionID(this.ParentWindow.GraphNodes.Keys.ToArray());
            UmoNode nodeToAdd = new UmoNode(UmoNodeType.file, "new file", additionKey, this.selectedNode.Index);
            AddNodeToSchema(additionKey, nodeToAdd);
        }

        public void AddFolder()
        {
            int additionKey = UmoNodesTreeView.GetAdditionID(this.ParentWindow.GraphNodes.Keys.ToArray());
            UmoNode nodeToAdd = new UmoNode(UmoNodeType.folder, "new folder", additionKey , this.selectedNode.Index);
            AddNodeToSchema(additionKey, nodeToAdd);
        }

        //var icon = System.Drawing.Icon.ExtractAssociatedIcon(fileName);
        //var bmp = icon.ToBitmap()

        private void AddNodeToSchema(int additionKey, UmoNode nodeToAdd)
        {
            this.ParentWindow.GraphNodes.Add(additionKey, nodeToAdd);
            this.UpdateTreeView();
            this.ParentWindow.UpdateGraph(true);
        }

        public void AddUrl()
        {
            int additionKey = UmoNodesTreeView.GetAdditionID(this.ParentWindow.GraphNodes.Keys.ToArray());
            UmoNode nodeToAdd = new UmoNode(UmoNodeType.url, "new url", additionKey, this.selectedNode.Index);
            AddNodeToSchema(additionKey, nodeToAdd);
        }
        private void AddFileButton_Click(object sender, RoutedEventArgs e)
        {
            AddFile();
            //UmoAddFileDialog addFile = new UmoAddFileDialog();
            //addFile.ShowDialog();
        }
        private UmoNode selectedNode;
        private void nodesView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (this.nodesView.SelectedItem != null)
            {
                TreeViewItem selItem = (TreeViewItem)this.nodesView.SelectedItem;
                UmoNode selNode = (UmoNode)selItem.Tag;
                this.selectedNode = selNode;
                if (selNode.NodeType != UmoNodeType.start)
                {
                    this.EnableItemsDeletion();
                }
                else
                {
                    this.DisableItemsDeletion();
                }
                if (selNode.NodeType == UmoNodeType.file || selNode.NodeType == UmoNodeType.url)
                {
                    this.DisableAddingItems();
                    this.DisableAddingFolders();
                }
                else
                {
                    this.EnableAddingItems();
                    this.EnableAddingFolders();
                }
            }
        }

        private void EnableItemsDeletion()
        {
            this.DeleteNodeButton.IsEnabled = true;
        }
        private void DisableItemsDeletion()
        {
            this.DeleteNodeButton.IsEnabled =false;
        }
        private void EnableAddingFolders()
        {
            this.AddFolderButton.IsEnabled = true;
        }
        private void EnableAddingItems()
        {
            this.AddFileButton.IsEnabled = true;
            this.AddUrlButton.IsEnabled = true;
        }

        private void AddUrlButton_Click(object sender, RoutedEventArgs e)
        {
            //UmoAddItemDialog addUrl = new UmoAddItemDialog();
            //addUrl.ShowDialog();
            AddUrl();
        }

        private void AddFolderButton_Click(object sender, RoutedEventArgs e)
        {
            //UmoAddItemDialog addFolder = new UmoAddItemDialog(true);
            //addFolder.ShowDialog();
            AddFolder();
        }

        private void AddLinkButton_Click(object sender, RoutedEventArgs e)
        {
            UmoAddRelationDialog addRel = new UmoAddRelationDialog();
            addRel.ShowDialog();
        }
    }
}
